package ext.tianma.part.finishedProduct.datautilities;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import wt.csm.navigation.ClassificationNode;
import wt.iba.definition.FloatDefinition;
import wt.iba.definition.IntegerDefinition;
import wt.iba.definition.StringDefinition;
import wt.iba.definition.TimestampDefinition;
import wt.log4j.LogR;
import wt.part.WTPart;
import wt.util.WTException;

import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.dataUtilities.DefaultDataUtility;
import com.ptc.core.components.rendering.guicomponents.AttributeInputComponent;
import com.ptc.core.components.rendering.guicomponents.ComboBox;
import com.ptc.core.components.rendering.guicomponents.DateInputComponent;
import com.ptc.core.components.rendering.guicomponents.MultiValuedInputComponent;
import com.ptc.core.components.rendering.guicomponents.NumericInputComponent;
import com.ptc.core.components.rendering.guicomponents.NumericInputComponent.Type;
import com.ptc.core.components.rendering.guicomponents.PushButton;
import com.ptc.core.components.rendering.guicomponents.StringInputComponent;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.com.core.CoreUtil;
import ext.tianma.MaterialClassification.ReadClassification;
import ext.tianma.part.finishedProduct.util.CSMUtil;
import ext.tianma.part.finishedProduct.util.IBAUtil;
import ext.tianma.part.finishedProduct.vo.ClassAttrRuleVo;
import ext.tianma.part.util.IbaAttributeUtil;


public class ClassIBAsDataUtility extends DefaultDataUtility{
	private static Logger log = LogR.getLogger(ClassIBAsDataUtility.class.getName());
	@Override
	public Object getDataValue(String paramString, Object obj, ModelContext modelContext) throws WTException {
		//获取部件分类节点
		ClassificationNode node = CSMUtil.getClassNodeByPart((WTPart) obj);
		if(null == node){
			String classNodeOid = modelContext.getNmCommandBean().getRequest().getParameter("part_classify_hidden");
			if(null == classNodeOid || StringUtils.isEmpty(classNodeOid)){
				throw new WTException("获取分类异常");
			}
			node = (ClassificationNode) CoreUtil.getWTObjectByOid(classNodeOid);
		}
		//获取组件的信息,如下拉列表,约束
		ClassAttrRuleVo classAttrRuleVo = new ClassAttrRuleVo();
		try {
			classAttrRuleVo = CSMUtil.getClassAttrInfo(node, paramString);
			log.debug("ClassIBAsDataUtility::partNumber"+((WTPart) obj).getNumber()+"paramString:"+paramString+";;classAttrRuleVo:"+classAttrRuleVo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Object component = super.getDataValue(paramString, obj, modelContext);
		
		String attributeValue = IbaAttributeUtil.getAttributeValue(obj, paramString);
		log.debug("attributeValue:"+attributeValue);
		if(null != classAttrRuleVo.getLegalValueList() && classAttrRuleVo.getLegalValueList().size()>0){
			ArrayList<String> valueList = (ArrayList<String>) classAttrRuleVo.getLegalValueList();
			valueList.add("");
			if(classAttrRuleVo.getSingleValued()){
				ComboBox combobox = new ComboBox(valueList, valueList, new ArrayList());
				combobox.setId(paramString);
				combobox.setColumnName(paramString);
				combobox.setLabel(classAttrRuleVo.getDisplayName());
				combobox.setRequired(classAttrRuleVo.getRequired());
				combobox.setSelected(attributeValue);
				component = combobox;
			}else{
				component = getMultiStringInputGUI(paramString,obj,modelContext,classAttrRuleVo,valueList);
			}
		}else if(null != classAttrRuleVo.getIntArray() && classAttrRuleVo.getIntArray().length>0){
			ArrayList<String> valueList = new ArrayList<String>();
			for(Integer intTemp : classAttrRuleVo.getIntArray()){
				valueList.add(String.valueOf(intTemp));
			}
			//valueList.add("");
			if(classAttrRuleVo.getSingleValued()){
				NumericInputComponent ricComponent = new NumericInputComponent(paramString,valueList, valueList, false);
				ricComponent.setId(paramString);
				ricComponent.setColumnName(paramString);
				ricComponent.setLabel(classAttrRuleVo.getDisplayName());
				ricComponent.setRequired(classAttrRuleVo.getRequired());
				ricComponent.setValue(StringUtils.isEmpty(attributeValue)?0:Integer.parseInt(attributeValue));
				component = ricComponent;
			}else{
				component = getMultiStringInputGUI(paramString,obj,modelContext,classAttrRuleVo,valueList);
			}
		}else if(StringDefinition.class.getName().equals(classAttrRuleVo.getDataType())){
			if(classAttrRuleVo.getSingleValued()){
				StringInputComponent stringInput = new StringInputComponent();
				stringInput.setId(paramString);
				stringInput.setColumnName(paramString);
				stringInput.setLabel(classAttrRuleVo.getDisplayName());
				stringInput.setRequired(classAttrRuleVo.getRequired());
				stringInput.setValue(attributeValue);
				component = stringInput;
			}else{
				component =	getMultiStringInputGUI(paramString,obj,modelContext,classAttrRuleVo,null);
			}
		}else if(FloatDefinition.class.getName().equals(classAttrRuleVo.getDataType())){
			if(classAttrRuleVo.getSingleValued()){
				NumericInputComponent ricComponent = new NumericInputComponent();
				ricComponent.setId(paramString);
				ricComponent.setColumnName(paramString);
				ricComponent.setLabel(classAttrRuleVo.getDisplayName());
				ricComponent.setRequired(classAttrRuleVo.getRequired());
				ricComponent.setType(Type.FLOAT);
				ricComponent.setValue(StringUtils.isEmpty(attributeValue)?0:Double.parseDouble(attributeValue));
				component = ricComponent;
			}else{
				component =	getMultiStringInputGUI(paramString,obj,modelContext,classAttrRuleVo,null);
			}
		}else if(IntegerDefinition.class.getName().equals(classAttrRuleVo.getDataType())){
			if(classAttrRuleVo.getSingleValued()){
				NumericInputComponent ricComponent = new NumericInputComponent();
				ricComponent.setId(paramString);
				ricComponent.setColumnName(paramString);
				ricComponent.setLabel(classAttrRuleVo.getDisplayName());
				ricComponent.setRequired(classAttrRuleVo.getRequired());
				ricComponent.setType(Type.INTEGER);
				ricComponent.setValue(StringUtils.isEmpty(attributeValue)?0:Integer.parseInt(attributeValue));
				component = ricComponent;
			}else{
				component =	getMultiStringInputGUI(paramString,obj,modelContext,classAttrRuleVo,null);
			}
		}else if(TimestampDefinition.class.getName().equals(classAttrRuleVo.getDataType())){
			DateInputComponent dataInput = new DateInputComponent();
			dataInput.setId(paramString);
			dataInput.setColumnName(paramString);
			dataInput.setLabel(classAttrRuleVo.getDisplayName());
			dataInput.setRequired(classAttrRuleVo.getRequired());
			attributeValue = attributeValue.replace("-", "/");
			dataInput.setValue(attributeValue);
			component = dataInput;
		}
	   return component;
	}
	
	private Object getMultiStringInputGUI(String s, Object obj, ModelContext modelcontext,
			ClassAttrRuleVo classAttrRuleVo,ArrayList<String> valueList)
			throws WTException {
		StringInputComponent defaulComponent = new StringInputComponent(s + "_dafaul",valueList,valueList);
		defaulComponent.setId(s + "_dafaul");
		defaulComponent.setName(s + "_dafaul");
		
		ArrayList<AttributeInputComponent> arraylist = getStringInputValue(s, obj,modelcontext,classAttrRuleVo,valueList);
		int i = 0;
		if (arraylist != null) {
			if (arraylist.size() < 2) {
				i = 1;
			} else {
				i = arraylist.size() - 1;
			}
			while (arraylist.size() < 2) {
				StringInputComponent component = new StringInputComponent(s
						+ "_" + arraylist.size(),valueList,valueList);
				component.setId(s + "_" + arraylist.size());
				component.setName(s + "_" + arraylist.size());
				component.setReadOnly(true);
				arraylist.add(component);
			}
		}
		MultiValuedInputComponent multiComponent = new MultiValuedInputComponent(
				arraylist, defaulComponent);
		PushButton minusButton = multiComponent.getMinusButton();
		minusButton.setId(s + "_minus");
		minusButton.setName(s + "_minus");
		minusButton.setComponentHidden(false);
		PushButton plusButton = multiComponent.getPlusButton();
		plusButton.setId(s + "_plus");
		plusButton.setName(s + "_plus");
		plusButton.setComponentHidden(false);
		multiComponent.setVisibleElements(i);
		multiComponent.setMultiValued(true);
		multiComponent.setRequired(classAttrRuleVo.getRequired());
		return multiComponent;
	}

	private ArrayList<AttributeInputComponent> getStringInputValue(String s, Object obj,
			ModelContext modelcontext,ClassAttrRuleVo classAttrRuleVo,ArrayList<String> valueList) throws WTException {
		ArrayList<AttributeInputComponent> arraylist = new ArrayList<AttributeInputComponent>();
		NmCommandBean commandBean = modelcontext.getNmCommandBean();
		Object object = commandBean.getPageOid().getRefObject();
		if (object instanceof WTPart) {
			WTPart part = (WTPart) object;
			List valuelist = IBAUtil.getIBAStringMultiValue(part, s);
			for (int i = 0; i < valuelist.size() + 1; i++) {
				if (i < 1) {
					StringInputComponent component = new StringInputComponent(
							s + "_" + i,valueList,valueList);
					component.setId(s + "_" + i);
					component.setName(s + "_" + i);
					if(valuelist.size() == 0){
						component.setValue("");
					}else{
						component.setValue(String.valueOf(valuelist.get(0)));
					}
					component.setReadOnly(true);
					arraylist.add(component);
				} else if (i == 1) {
					continue;
				} else if (i > 1) {
					StringInputComponent component = new StringInputComponent(
							s + "_" + i,valueList,valueList);
					component.setId(s + "_" + i);
					component.setName(s + "_" + i);
					component.setValue(valuelist.get(i - 1).toString());
					component.setReadOnly(true);
					arraylist.add(component);
				}
			}
			StringInputComponent component = new StringInputComponent(s
					+ "_1",valueList,valueList);
			component.setId(s + "_1");
			component.setName(s + "_1");
			component.setReadOnly(true);
			arraylist.add(component);
		}
		return arraylist;
	}
	
}
